package com.applabs.dnd.core;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class DNDManager {
	
	private final static String TAG = "DNDManager";
	private static DNDManager mInstance=null;
	private static Context mContext=null;
	
	private DNDManager(Context context)
	{
		mContext = context;
	}
	
	public static DNDManager getInstance(Context context)
	{
		if(mInstance==null)
		{
			Log.d(TAG,"Creating Instance");
			mContext = context;
			mInstance = new DNDManager(context);
		}
		return mInstance;
	}
	
	public boolean startService()
	{
		Log.d(TAG,"startService entry");
		mContext.startService(new Intent(mContext, DNDMeService.class));
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mContext);
		pref.edit().putBoolean("DNDEnable", true).commit();
		return true;
	}
	
	public boolean stopService()
	{
		Log.d(TAG,"stopService entry");
		mContext.stopService(new Intent(mContext, DNDMeService.class));
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mContext);
		pref.edit().putBoolean("DNDEnable", false).commit();
		return true;
	}
	
	public boolean isServiceEnabled()
	{
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mContext);
		return pref.getBoolean("DNDEnable", false);
	}
	
	public boolean isServiceRunning()
	{
		Log.d(TAG,"isServiceRunning entry");
		 ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
	        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
	            if ("com.applabs.dnd.core.DNDMeService".equals(service.service.getClassName())) {
	            	Log.d(TAG,"DNDService Running");
	                return true;
	            }
	        }
	     return false;
	}

}
